﻿// <copyright file="action_Team_News.aspx.cs" company="NTalent">
// Copyright (c) 2010, NTalent, Inc. All rights reserved
// </copyright>
// <author>卢俊坤</author>
// <author>王彦博 符茂松</author>

using System;
using Net.Ntalent.Action50.Core.Controls;
using Net.Ntalent.Action50.Core.Service;
using Net.Ntalent.Action50.Core.Util;
using Net.Ntalent.Action50.DataAccess;

/// <summary>
/// 显示某个action的某一篇news的页面
/// </summary>
///<author>王彦博</author> 
/// <copyright>
/// Copyright (c) 2010, NTalent, Co. Ltd. All rights reserved.
/// </copyright>
/// <version>1.0.0</version>
public partial class action_Team_News : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[Constants.SESSION_KEY_ADMIN_NAME] == null)
        {
            Li_Admin.Visible = false;
            Li_Login.Visible = true;
        }
        else
        {
            Li_Admin.Visible = true;
            Li_Login.Visible = false;
        }

        if (Helper.getQueryString(Context, "newsId") == string.Empty)
        {
            //如果查询字符串中没有actionId
            return;
        }

        ReadOnlyService readOnlyService = new ReadOnlyService();
        long logsId = Convert.ToInt64(Helper.getQueryString(Context, "newsId"));
        long actionId = Helper.getActionIdFromNewsAndLogsId(logsId);
        T_NEWS_AND_LOGS logs = readOnlyService.RetrieveNewsAndLogs(logsId);

        //加载团队照片,团队介绍
        T_ACTIONS action = readOnlyService.RetrieveAction(actionId);
        Literal_actionShortIntro.Text = action.SHORT_INTRO;
        long landScapeId = Helper.getActionLandscapeId(action.Id);
        if (landScapeId == 0)
        {
            //如果该action没有landscape，则显示默认图片
            Image_actionLandScape.ImageUrl = Helper.DEFAULT_LANDSCAPE;
        }
        else
        {
            Image_actionLandScape.ImageUrl = 
                "Handlers/GallaryHandler.ashx?action=getImage&id=" + landScapeId;
        }

        //加载文章信息
        Literal_LogsTitle.Text = logs.POST_DATETIME.ToString("M/dd") + " - " + logs.POST_TITLE;
        Literal_Datetime.Text = logs.POST_DATETIME.ToString("MMM d, yyyy");
        Literal_LogsContent.Text = logs.POST_CONTENT;
    }

    /// <summary>
    /// 留言
    /// </summary>
    protected void LinkButton_LeaveWords_Click(object sender, EventArgs e)
    {
        //已经在客户端进行了非空验证。
        string checkCode = Text_CheckCode.Text.Trim();
        AuthenticationService authenticationService = new AuthenticationService();
        if(!authenticationService.CompareCheckCodes(checkCode))
        {
            Label_checkCodeMessage.Text = "Check code error";
            span_info.Text = string.Empty;
            return;
        }

        string email = TextBox_UserEmail.Text.Trim();
        long logsId = Convert.ToInt64(Request.QueryString["newsId"]);
        long actionId = Helper.getActionIdFromNewsAndLogsId(logsId);
        string userIp = Request.UserHostAddress;
        string leavedWords = HTMLFilter.Strip(tb_Leavewords.Text.Trim());

        var editService = new EditService();  
        editService.LeaveWords(logsId, actionId, leavedWords, userIp, email);
        Response.Redirect(Request.UrlReferrer.ToString());

    }

    protected void ListView1_DataBound(object sender, EventArgs e)
    {
        //如果数据不足一页，则不显示分页控件
        var readOnlyService = new ReadOnlyService();
        long logsId = Convert.ToInt64(Helper.getQueryString(Context, "newsId"));
        long leavedWordsCount = readOnlyService.RetrieveWords(logsId).Count;
        if (leavedWordsCount <= 10)
        {
            DataPager1.Visible = false;
        }
    }

    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        new AuthenticationService().LogOut();
        Response.Redirect(Request.UrlReferrer.ToString());
    }
}

